using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

namespace Ramparts
{
    class OptionsMenu : Scene
    {
        Rectangle viewportRect;
        SpriteFont gameFont;
        int selectedItem = 0;
        double time;
        KeyboardState oldState;
        

        List<MenuItem> menuItems = new List<MenuItem>();
        
        public OptionsMenu(SceneManager sceneMgr)
        {
            sceneManager = sceneMgr;
        }

        public override bool initScene()
        {

            base.initScene();
            backgroundTexture = sceneManager.rampart.content.Load<Texture2D>("Content\\title_bg");
            gameFont = sceneManager.rampart.content.Load<SpriteFont>("Content\\Fonts\\MainGameFont");


            //This is for adding my menu items
            MenuItem music = new MenuItem("Music Volume", new Vector2(320.0f, 250.0f));
            MenuItem effects = new MenuItem("SoundFX Volume", new Vector2(320.0f, 300f));
            MenuItem back = new MenuItem("Back", new Vector2(320f, 350f));

            menuItems.Add(music);
            menuItems.Add(effects);
            menuItems.Add(back);

            //Create a Rectangle that represents the full
            //drawable area of the game screen.
            viewportRect = new Rectangle(0, 0,
                sceneManager.rampart.graphics.GraphicsDevice.Viewport.Width,
                sceneManager.rampart.graphics.GraphicsDevice.Viewport.Height);
            oldState = Keyboard.GetState();

            // Set the camera for the main menu
            camera.ManualCamera = false;
            camera.Position = new Vector2(-1*viewportRect.Width/2, -1*viewportRect.Height/2);



            return true;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
          
            time = gameTime.TotalGameTime.TotalSeconds;

            KeyboardState currentState = Keyboard.GetState();

            if (currentState.IsKeyDown(Keys.Down))
            {
                if (!oldState.IsKeyDown(Keys.Down))
                {
                    selectedItem++;
                }
            }

            if (currentState.IsKeyDown(Keys.Up))
            {
                if (!oldState.IsKeyDown(Keys.Up))
                {
                    selectedItem--;
                }
            }

                
            if (currentState.IsKeyDown(Keys.Right))
            {
                if (selectedItem == 0)
                {
                    sceneManager.backgroundMusicVolume = MathHelper.Clamp(sceneManager.backgroundMusicVolume += .04f, 0.01f, 2f);
                }
                if (selectedItem == 1)
                {
                    sceneManager.effectsVolume = MathHelper.Clamp(sceneManager.effectsVolume += .04f, 0.01f, 2f);
                }
            }


            if (currentState.IsKeyDown(Keys.Left))
            {
                if (selectedItem == 0)
                {
                    sceneManager.backgroundMusicVolume = MathHelper.Clamp(sceneManager.backgroundMusicVolume -= .04f, 0.01f, 2f);
                }
                if (selectedItem == 1)
                {
                    sceneManager.effectsVolume = MathHelper.Clamp(sceneManager.effectsVolume -= .04f, 0.01f, 2f);
                }
            }



            // roll around to the top or bottom if we get that far
            if (selectedItem == 3)
            {
                selectedItem = 0;
            }
            if (selectedItem == -1)
            {
                selectedItem = 2;
            }

            if (currentState.IsKeyDown(Keys.Escape))
            {
                sceneManager.RemoveScene();
            }

            if (currentState.IsKeyDown(Keys.Enter))
            {
                if (!oldState.IsKeyDown(Keys.Enter))
                {
                    if (selectedItem == 2)
                    {
                        sceneManager.RemoveScene();
                    }
                }
            }

            oldState = currentState;

        }

        public override void drawScene(){


            base.drawScene();

           sceneManager.rampart.spriteBatch.Draw(backgroundTexture, viewportRect, Color.White);

            for (int i = 0; i < 3; i++)
            {
                Color itemColor = Color.White;
                float scale = 1;
                if (i == selectedItem)
                {
                    itemColor = Color.Yellow;
                    float pulsate = (float)Math.Sin(time * 6) + 1;
                    scale = 1 + 0.1f * pulsate;

                }
                sceneManager.rampart.spriteBatch.DrawString(gameFont, menuItems[i].GetText(),
                                                            menuItems[i].GetPosition(), itemColor, 0f, 
                                                            new Vector2(0,gameFont.LineSpacing /2),scale,SpriteEffects.None,0);

                if (i == 0)
                {
                    sceneManager.rampart.spriteBatch.DrawString(gameFont, ((int)(sceneManager.backgroundMusicVolume*50)).ToString(),
                                                        new Vector2(menuItems[0].GetPosition().X + 150,
                                                                    menuItems[0].GetPosition().Y), itemColor, 0f,
                                                        new Vector2(0, gameFont.LineSpacing / 2), scale, SpriteEffects.None, 0);
                }

                if (i == 1)
                {
                    sceneManager.rampart.spriteBatch.DrawString(gameFont, ((int)(sceneManager.effectsVolume * 50)).ToString(),
                                                        new Vector2(menuItems[1].GetPosition().X + 200,
                                                                    menuItems[1].GetPosition().Y), itemColor, 0f,
                                                        new Vector2(0, gameFont.LineSpacing / 2), scale, SpriteEffects.None, 0);
                }

            }

            

            sceneManager.rampart.spriteBatch.End();
        }
    }
}
